package com.jefe;
import com.liferay.portal.SystemException;
import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import com.jefe.service.model.*;
import com.jefe.service.service.*;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.portlet.PortletFileUpload;
import org.apache.commons.io.output.DeferredFileOutputStream;
import java.io.File;

/**
 * Welcome Portlet Class
 */
public class Welcome extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
        String success = null;
        String error = null;

        PortletPreferences prefs = request.getPreferences();
        if(request.getParameter("form").equals("select")){
            //selection is made from the drop down
            String choice = request.getParameter("choice");
            prefs.setValue("id", choice);
            try{
                prefs.store();
                success = "Your selection is been saved successfully.";
            }catch(Exception ex){
                ex.printStackTrace();
                error = ex.getMessage();
            }
        }else if(request.getParameter("form").equals("add")){
            //new message is required to add to the database
            String heading = request.getParameter("heading");
            String message = request.getParameter("message");
            String shortName = request.getParameter("shortName");
            String image = request.getParameter("image").replace("/", "\\");
            System.out.println("heading is: "+heading);
            int count = 0;
            try {
                count = MessageTblLocalServiceUtil.getMessageTblsCount();
                System.out.println("count is:"+count);
            } catch (SystemException ex) {
                error = ex.getMessage();
            }
            int id = count+1;
            MessageTbl table = MessageTblLocalServiceUtil.createMessageTbl(id);
            System.out.println("Table entry is created.");
            table.setId(id);
            table.setHeading(heading);
            table.setMessage(message);
            table.setShortName(shortName);
            table.setImage(image);
            String pref_id = Integer.toString(id);

            try {
                MessageTblLocalServiceUtil.addMessageTbl(table);
                prefs.setValue("id", pref_id);
                try{
                    prefs.store();
                }catch(Exception e){
                    e.printStackTrace();
                }
                success = "The record was successfully saved.";
                System.out.println("Table is added.");
            } catch (SystemException ex) {
                error = "Could not add the record to the database. "+ex.getMessage();
            }
        }else{

        }
        if(success!=null){response.setRenderParameter("success", URLEncoder.encode(success, "UTF-8"));}
        if(error!=null){response.setRenderParameter("error", URLEncoder.encode(error, "UTF-8"));}
    }
    
    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/Welcome_view.jsp");
        dispatcher.include(request, response);
    }
    public void doEdit(RenderRequest request,RenderResponse response) throws PortletException,IOException {
            response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/Welcome_edit.jsp");
        dispatcher.include(request, response);
    }
}